var token_config = require('../config').token;
var jwt = require('jsonwebtoken');

exports.gen_token = function (user_id, user_role) {
    return new Promise((resolve, reject) => {
        const token = jwt.sign({
            user_id: user_id,
            user_role: user_role
        }, token_config.signkey, {expiresIn: token_config.expire_time});
        resolve(token);
    })
}

exports.ver_token = function (token) {
    return new Promise((resolve, reject) => {
        var info = jwt.verify(token, token_config.signkey);
        resolve(info);
    }).then(data => {
        return data;
    }).catch(error => {
        throw error;
    })
}